﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinessObject;
using Entity;
using System.IO;

namespace GreenLife.Admin
{
    public partial class ChinhSuaTienDo : System.Web.UI.Page
    {
        DataSet ds;
        News n;
        NewsBO nBO = new NewsBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                selectNewByID();
            }
        }

        public void selectNewByID()
        {
            n = new News();
            int id = Int32.Parse(Request.QueryString["id"]);
            n = nBO.selectNewByID(id);
            txtID.Text = n.New_ID.ToString();
            txtTitle.Text = n.New_Title.ToString();
            txtQuickTitle.Text = n.New_QuickTitle.ToString();
            imgNews.ImageUrl = n.New_Image.ToString();
            txtContent.Text = n.New_Content.ToString();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string imgFileName = Path.GetFileName(FileUploadControl.FileName);
            n = new News();
            n.New_Title = txtTitle.Text;
            n.New_QuickTitle = txtQuickTitle.Text;
            n.New_NStatus = 2;
            n.New_Image = "~/imageUpload/" + imgFileName;
            n.New_ID = Int32.Parse(txtID.Text);
            n.New_Content = txtContent.Text;
            //Response.Redirect("error.aspx?id=" + FileUploadControl.FileName);
            if (imgFileName.Equals("") == true)
            {
                imgFileName = "greenlifelogo.jpg";
            }
            else
            {
                FileUploadControl.SaveAs(Server.MapPath("~/imageUpload/") + imgFileName);
            }
            if (nBO.updateNews(n))
            {
                Response.Redirect("DanhSachTienDo.aspx");
            }
            else
            {
                Response.Redirect("error.aspx");
            }
        }
    }
}